package com.droidnow.droidaudio;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import com.droidnow.adapter.PlayAdapter;
import com.droidnow.common.Request;
import com.droidnow.common.XMLParser;
import com.droidnow.model.Rss;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: tinnd
 * Date: 4/16/13
 * Time: 6:28 PM
 * To change this template use File | Settings | File Templates.
 */
public class PlayListActivity extends Activity {
	ListView list;
	PlayAdapter adapter;
	AudioApplication audioApp;
	private int selection = -1;
	private ArrayList<HashMap<String, String>> playList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playlist);
		audioApp = ((AudioApplication) PlayListActivity.this.getApplication());
		mp = audioApp.mp;
		isPLAYING = audioApp.isPLAYING;

		this.initialize();
	}

	private boolean isPLAYING = false;
	private Button btnPlayAll;
	private MediaPlayer mp;
//
//	private void stopPlaying() {
//		this.mp.release();
//		this.mp = null;
//	}

	private void initialize() {

		XMLParser parser = new XMLParser();
		Intent intent = getIntent();
		if (intent.getExtras().getString(Request.INTENT_PLAY_TYPE).equals(Request.PLAY_BOOK)) {
			Log.e("DROID-PLAY------------PLAY_CASE", "BOOK");
			String bookId = intent.getExtras().getString(Request.INTENT_PLAY_ID);
			playList = Rss.GetAudioBookList("http://media.tuoitre.vn/Playlist.aspx?Book=" + bookId);
		} else {
			playList = Rss.GetAudioNewsList();
		}
		Log.e("DROID-PLAY------------PARSE SUCCESS", "SUCCESS!!");

		list = (ListView) findViewById(R.id.lstPlaylist);

		// Getting adapter by passing xml data ArrayList
		adapter = new PlayAdapter(this, playList);
		list.setAdapter(adapter);


		// Click event for single list row
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				HashMap<String, String> doc;
				doc = playList.get(position);
				String itemId = doc.get(Rss.KEY_ID);


				XMLParser parser = new XMLParser();
				String xml;
				Intent intent = getIntent();
				if (intent.getExtras().getString(Request.INTENT_PLAY_TYPE).equals(Request.PLAY_BOOK)) {
					String bookId = intent.getExtras().getString(Request.INTENT_PLAY_ID);
					String urlBook = doc.get(Rss.KEY_THUMB_URL);
					setPlay(urlBook);

					Log.e("DROID-PLAY------------PLAY_CASE", " SUCCESS! NOW PLAYING!");
					updateList(position,Rss.KEY_STATUS_PLAYING);

//					ImageView imgPlayMark;
//					View viewItem;
//					viewItem = parent.getChildAt(position);
//					imgPlayMark = (ImageView) viewItem.findViewById(R.id.imgPlayMark);
//					imgPlayMark.setVisibility(View.VISIBLE);

//					selection = position;

				} else {
					xml = parser.getXmlFromUrl("http://media.tuoitre.vn/Playlist.aspx?Media=" + itemId);
					Document domPlayList = parser.getDomElement(xml); // getting DOM element
					NodeList nl = domPlayList.getElementsByTagName("REF");
					Element element = (Element) nl.item(0);
					Log.e("DROID-PLAY-FILE", element.getAttribute("HREF"));

					String url = element.getAttribute("HREF");
					AudioApplication audioApp = ((AudioApplication) PlayListActivity.this.getApplication());
					setPlay(url);

				}

			}
		});
	}

	private void updateList(int position, String statusMark) {

		int index = list.getFirstVisiblePosition();
		View v = list.getChildAt(0);
		int top = (v == null) ? 0 : v.getTop();
		int i = 0;

		for (HashMap<String, String> doc : playList) {
			if (i == position)
				doc.put(Rss.KEY_STATUS, statusMark);
			else if (!doc.get(Rss.KEY_STATUS).equals(Rss.KEY_STATUS_NONE))
				doc.put(Rss.KEY_STATUS, Rss.KEY_STATUS_NONE);
			i++;
		}
		list.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		Log.e("DROID-PLAY------------updateList", String.valueOf(position));
		list.setSelectionFromTop(index, top);
	}

	public void setPlay(String url) {
		if (isPLAYING) {
			isPLAYING = false;
			stopPlaying();
		}
		isPLAYING = true;
		mp = new MediaPlayer();
		//mp.reset();

		Log.e("DROID-PLAY-ISPLAY", url);
		try {
			mp.setDataSource(url);
			mp.prepare();
			mp.start();
		} catch (IOException ex) {
			Log.e("DROID-PLAY-MP3", "AUDIO prepare() failed::" + ex);
		}
	}

	public void stopPlaying() {
		this.mp.release();
		this.mp = null;
	}
}